/**
 *Creates an instance of Roaming.
 * @param {*} viewer 需要传入
 * @param {*} options.modeluri 模型的uri 需要传入
 * @param {*} options.time 漫游时间  需要传入
 * @param {*} options.points  点集合 需要传入
 * @param {*} options.isPathShow 路径是否显示 需要传入
 * @memberof Roaming
 */
VFG.PlanRoam = function(viewer,options) {
	this.options = options;
	this.viewer = viewer;
	this.entity = undefined;
	this.start = undefined;
	this.stop = undefined;
	this.map = new Map;
	this.index = 0;
	this.modeluri = options.modeluri ? options.modeluri : undefined;
	this.isLock = options.isLock ? options.isLock : false;
	this.time = options.duration; //传入飞行时间
	this.lines = options.lines;
	this.points = []; //传入漫游点位
	this.getLnLaFormC3();
	this.length = this.getTotalLength(this.points); //计算线路长度
	this.speed = this.length / this.time; //计算飞行速度
	this.startPoint = this.points[0];
	this.stopPoint = this.points[1];
	this.isPathShow = options.isPathShow ? options.isPathShow : false;
	this.property = this.GetRoamingLineProperty();
	this.InitRoaming();
}

VFG.PlanRoam.prototype.flyToPoisition = function(position) {
	if(position){
		this.viewer.camera.flyTo({//定位到范围中心点
			  destination: Cesium.Cartesian3.fromDegrees(position.x*1, position.y*1,(position.z*1)+1000),
			  orientation: {
			     heading: Cesium.Math.toRadians(180),//1
			     pitch: Cesium.Math.toRadians(-90),
			     roll: 0
			  }
		});
	}
}

/**
 * 坐标转换
 */
VFG.PlanRoam.prototype.getLnLaFormC3 = function(){
	let _this=this;
	if(this.lines && this.lines.length>0){
		for (let p of this.lines) {
			let point = VFG.Util.getLnLaFormC3(_this.viewer,p);
			if(point){
				_this.points.push(point);
			}
		}
	}
}

/**
 * 计算2点距离
 */
VFG.PlanRoam.prototype.getLength = function(startPoint, stopPoint) {
	let linePoint = [];
	linePoint.push([startPoint.x * 1, startPoint.y * 1]);
	linePoint.push([stopPoint.x * 1, stopPoint.y * 1]);
	return turf.length(turf.lineString(linePoint), {
		units: 'kilometers'
	});
}
/**
 * 计算多个点位总长度
 */
VFG.PlanRoam.prototype.getTotalLength = function(points) {
	let linePoint = [];
	for (let point of points) {
		let item = [];
		item.push(point.x * 1);
		item.push(point.y * 1);
		linePoint.push(item);
	}
	if (linePoint.length > 0) {
		let line = turf.lineString(linePoint);
		return turf.length(line, {
			units: 'kilometers'
		});
	}
	return 0;
}
VFG.PlanRoam.prototype.GetRoamingLineProperty = function(startPoint, endPoint) {
	let property = new Cesium.SampledPositionProperty();
	let lineLength = this.points.length;
	let start = Cesium.JulianDate.now()
	this.start = start;
	let stop = Cesium.JulianDate.addSeconds(start, this.time, new Cesium.JulianDate());
	this.stop = stop;
	this.viewer.clock.startTime = start.clone();
	this.viewer.clock.stopTime = stop.clone();
	this.viewer.clock.currentTime = start.clone();
	this.viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; // Loop at the end
	this.viewer.clock.multiplier = 10;
	this.viewer.clock.onStop.addEventListener(this.options.complete);
	let time = Cesium.JulianDate.addSeconds(start, 0, new Cesium.JulianDate());
	let j=0;
	for (i = 0; i < lineLength; i++) {
		if(i>0)
		time = Cesium.JulianDate.addSeconds(start, j+=(this.time/lineLength), new Cesium.JulianDate());
		property.addSample(time, Cesium.Cartesian3.fromDegrees(this.points[i].x, this.points[i].y, this.points[i].z));
	}
	return property;
}
/**
 * 计算时间
 */
VFG.PlanRoam.prototype.ComputeTime = function(point1, point2) {
	let linePoint = [];
	linePoint.push([point1.x * 1, point1.y * 1]);
	linePoint.push([point2.x * 1, point2.y * 1]);
	let line = turf.lineString(linePoint);
	return turf.length(line, {
		units: 'kilometers'
	}) / this.speed;
}

/**
 * @param {*} 运行
 * @memberof Roaming
 */
VFG.PlanRoam.prototype.InitRoaming = function() {
	var _this = this;
	var position = _this.property;
	this.entity = this.viewer.entities.add({
		availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
			start: this.start,
			stop: this.stop
		})]),
		position: position,
		orientation: new Cesium.VelocityOrientationProperty(position),
		point: {
			pixelSize: 10,
			color: Cesium.Color.YELLOW
		},
		path: {
			resolution: 1,
			material: new Cesium.PolylineGlowMaterialProperty({
				glowPower: 0.1,
				color: Cesium.Color.YELLOW
			}),
			width: 10,
			show: this.isPathShow
		}
	})
	this.entity.position.setInterpolationOptions({ // 点插值
		interpolationDegree: 5,
		interpolationAlgorithm: Cesium.LinearApproximation
	});
	this.flyToPoisition(_this.points[0]);
	this.viewer.clock.shouldAnimate = false;
	this.viewer.trackedEntity = this.entity;
}
/**
 * 开始巡检
 */
VFG.PlanRoam.prototype.StartRoaming = function() {
	this.viewer.clock.shouldAnimate = true;
	this.viewer.clock.onStop.addEventListener(this.OnStopRoaming, this);
	this.viewer.clock.shouldAnimate = true;
}
VFG.PlanRoam.prototype.OnStopRoaming = function(e) {
	let _this = this;
	this.viewer.clock.shouldAnimate = false;
	_this.viewer.clock.onStop.removeEventListener(_this.OnStopRoaming, this);
	if (_this.options.complete) {
		_this.options.complete(this);
	}
}
/**
 *漫游的暂停和继续
 * @param {*} state bool类型 false为暂停，ture为继续
 * @memberof Roaming
 */
VFG.PlanRoam.prototype.PauseOrContinue = function(state) {
	this.viewer.clock.shouldAnimate = state;
}
/**
 *改变飞行的速度
 * @param {*} value  整数类型
 * @memberof Roaming
 */
VFG.PlanRoam.prototype.ChangeRoamingSpeed = function(value) {
	this.viewer.clock.multiplier = value;
}

/**
 *取消漫游
 * @memberof Roaming
 */
VFG.PlanRoam.prototype.EndRoaming = function() {
	if (this.entity !== undefined) {
		this.viewer.clock.shouldAnimate = false;
		this.viewer.clock.onStop.removeEventListener(this.OnStopRoaming);
		this.viewer.entities.remove(this.entity);
	}
}

VFG.PlanRoam.prototype.destroy = function() {
	let _self = this;
	_self.EndRoaming();
	return Cesium.destroyObject(_self);
}